package sjtu.ist.warehouse4ws.service;


import java.util.ArrayList;
import java.util.List;

import sjtu.ist.warehouse4ws.dao.RoleDao;
import sjtu.ist.warehouse4ws.dao.RoleDaoHibernate;
import sjtu.ist.warehouse4ws.dao.cust.CustRoleDao;
import sjtu.ist.warehouse4ws.model.CustRole;
import sjtu.ist.warehouse4ws.model.Role;
import sjtu.ist.warehouse4ws.tool.Constants;
import sjtu.ist.warehouse4ws.tool.PropertyHelper;

public class RoleServiceImpl implements RoleService{
	private RoleDao roleDao;
	private CustRoleDao custRoleDao;

	public void setRoleDao(RoleDaoHibernate roleDao) {
		this.roleDao = roleDao;
	}


	public void setCustRoleDao(CustRoleDao custRoleDao) {
		this.custRoleDao = custRoleDao;
	}


	@Override
	public String addRole(Role role,int companyId) {
		if(companyId==Constants.ADMIN_COMPANY_ID){
			return this.roleDao.save(role);
		}else{
			CustRole cr=new CustRole();
			PropertyHelper.copyProperties(role, cr);
			return custRoleDao.save(cr);
		}
	}


	@Override
	public List<Role> getRoleListById(String id) {
		// TODO Auto-generated method stub
		return (List<Role>)roleDao.findById(id);
	}


	@Override
	public List<Role> getRoleList() {
		// TODO Auto-generated method stub
		return (List<Role>) roleDao.findAllRoles();
	}


	@Override
	public List<Role> getRoleListbyGroupId(String groupId,int companyId) {
		// TODO Auto-generated method stub
		if(companyId==Constants.ADMIN_COMPANY_ID){
			return roleDao.findAllRoles(groupId);
		}else{
			List<CustRole> clist=custRoleDao.getRoleListbyGroupId(groupId);
			List<Role> list=new ArrayList<Role>();
			for(CustRole cr : clist){
				Role r=new Role();
				PropertyHelper.copyProperties(cr, r);
				list.add(r);
			}
			return list;
		}
	}


	@Override
	public List<Role> getRoleListByUid(String uid) {
		// TODO Auto-generated method stub
		return roleDao.findByUid(uid);
	}
	
	@Override
	public List<Role> getRoleListByGroupAndVersion(String groupId,String versionId) {
		// TODO Auto-generated method stub
		return roleDao.findByGroupAndVersion(groupId,versionId);
	}

}
